package com.boruan.qq.zxgylibrary.service.presenter;

import android.content.Context;
import android.widget.TextView;

import com.boruan.qq.zxgylibrary.R;
import com.boruan.qq.zxgylibrary.base.BasePresenter;
import com.boruan.qq.zxgylibrary.base.BaseView;
import com.boruan.qq.zxgylibrary.service.manager.GXDrugModule;
import com.boruan.qq.zxgylibrary.service.model.AddressBackPriceBean;
import com.boruan.qq.zxgylibrary.service.model.AddressListBean;
import com.boruan.qq.zxgylibrary.service.view.AddressView;
import com.convenient.qd.core.bean.BaseResBean;
import com.convenient.qd.core.net.BaseHttpObserver;
import com.lljjcoder.Interface.OnCityItemClickListener;
import com.lljjcoder.bean.CityBean;
import com.lljjcoder.bean.DistrictBean;
import com.lljjcoder.bean.ProvinceBean;
import com.lljjcoder.citywheel.CityConfig;
import com.lljjcoder.style.citypickerview.CityPickerView;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static com.boruan.qq.zxgylibrary.ui.activities.AddAddressActivity.cityName;

/**
 * Created by 崔琦 on 2019/9/5.
 * des: 有关地址的p层逻辑
 */

public class AddressPresenter implements BasePresenter{

    private Context mContext;
    private AddressView addressView;
    private CityPickerView mPicker;

    /** 地址列表实体 */
    private List<AddressListBean.ResultBean> mAddressListBean;
    /** 判断选择的地址是否可配送实体，可配送会返回配送费 */
    private AddressBackPriceBean.ResultBean mAddressBackPriceBean;

    public AddressPresenter(Context mContext) {
        this.mContext = mContext;
    }

    @Override
    public void onCreate() {
        mPicker = new CityPickerView();
        mPicker.init(mContext);
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onStop() {
        addressView = null;
    }

    @Override
    public void pause() {

    }

    @Override
    public void attachView(BaseView view) {
        addressView = (AddressView)view;
    }

    public void selectCity(final TextView selectAddress){
        //配置
        CityConfig cityConfig = new CityConfig.Builder()
                .title("选择城市")//标题
                .titleTextSize(18)//标题文字大小
                .titleTextColor("#000000")//标题文字颜  色
                .titleBackgroundColor("#E9E9E9")//标题栏背景色
                .confirTextColor("#2f71fb")//确认按钮文字颜色
                .confirmText("确认")//确认按钮文字
                .confirmTextSize(16)//确认按钮文字大小
                .cancelTextColor("#2f71fb")//取消按钮文字颜色
                .cancelText("取消")//取消按钮文字
                .cancelTextSize(16)//取消按钮文字大小
                .setCityWheelType(CityConfig.WheelType.PRO_CITY_DIS)//显示类，只显示省份一级，显示省市两级还是显示省市区三级
                .showBackground(true)//是否显示半透明背景
                .visibleItemsCount(5)//显示item的数量
                .province("山东省")//默认显示的省份
                .city("青岛市")//默认显示省份下面的城市
                .district("市北区")//默认显示省市下面的区县数据
                .provinceCyclic(true)//省份滚轮是否可以循环滚动
                .cityCyclic(true)//城市滚轮是否可以循环滚动
                .districtCyclic(true)//区县滚轮是否循环滚动
//                .setCustomItemLayout(R.layout.item_city)//自定义item的布局
//                .setCustomItemTextViewId(R.id.item_city_name_tv)//自定义item布局里面的textViewid
                .drawShadows(true)//滚轮不显示模糊效果
                .setLineColor("#2f71fb")//中间横线的颜色
                .setLineHeigh(3)//中间横线的高度
                .setShowGAT(true)//是否显示港澳台数据，默认不显示
                .build();
        //添加配置
        mPicker.setConfig(cityConfig);
        //显示
        mPicker.showCityPicker();
        //监听选择点击事件及返回结果
        mPicker.setOnCityItemClickListener(new OnCityItemClickListener() {
            @Override
            public void onSelected(ProvinceBean province, CityBean city, DistrictBean district) {
                super.onSelected(province, city, district);
                cityName = city + "";
                //为TextView赋值
                selectAddress.setText(province + " " + city + " " + district);
                selectAddress.setTextColor(mContext.getResources().getColor(R.color.textColor));
            }

            @Override
            public void onCancel() {
                super.onCancel();
            }
        });

    }

    /**
     * 添加地址
     * */
    public void addAddress(String userName,String userPhone,String address,String detailAddress,String fullAddress,int isDefault,int sex,String cityName,int tag,double lat,double lng,String adCode){
        addressView.showProgress();
        GXDrugModule.getInstance().addAddress(userName, userPhone, address, detailAddress, fullAddress, isDefault, sex, cityName, tag,lat,lng, adCode,new BaseHttpObserver<BaseResBean>() {
            @Override
            public void onSuccess(BaseResBean body) {
                addressView.hideProgress();
                addressView.addAddressSuccess(body.getMessage());
            }

            @Override
            public void onFailure(int status, String msg) {
                addressView.hideProgress();
                if (status == 200){
                    addressView.addAddressSuccess(msg);
                }else {
                    addressView.addAddressFailed(msg);
                    //ToastUtils.showShort(msg);
                }
            }
        });
    }

    /**
     * 获取地址列表
     * */
    public void getAddressListData(String shopId){
        addressView.showProgress();
        GXDrugModule.getInstance().getAddressData(shopId, new BaseHttpObserver<BaseResBean<List<AddressListBean.ResultBean>>>() {
            @Override
            public void onSuccess(BaseResBean<List<AddressListBean.ResultBean>> addressListBean) {
                mAddressListBean = addressListBean.getResult();
                if (mAddressListBean != null){
                    addressView.getAddressListSuccess(mAddressListBean);
                }
                addressView.hideProgress();
            }

            @Override
            public void onFailure(int i, String s) {
                addressView.hideProgress();
                addressView.getAddressListFailed(s);
            }
        });

    }

    /**
     * 删除地址
     * */
    public void deleteAddress(int addressId){
        addressView.showProgress();
        GXDrugModule.getInstance().deleteAddress(addressId, new BaseHttpObserver<BaseResBean>() {
            @Override
            public void onSuccess(BaseResBean body) {
                addressView.hideProgress();
                addressView.deleteAddressSuccess(body.getMessage());
            }

            @Override
            public void onFailure(int status, String msg) {
                addressView.hideProgress();
                if (status == 200){
                    addressView.deleteAddressSuccess(msg);
                }else {
                    addressView.deleteAddressFailed(msg);
                }
            }
        });

    }

    /**
     * 修改地址信息
     * */
    public void modifyAddressInfo(int addressId,int sex,String userName, String userPhone, final String address, String detailAddress, String fullAddress, int isDefault, String cityName,int tag,double lat,double lng,String adCode){
        addressView.showProgress();
        GXDrugModule.getInstance().modifyAddress(addressId, sex, userName, userPhone, address, detailAddress, fullAddress, isDefault, cityName, tag,lat,lng,adCode, new BaseHttpObserver<BaseResBean>() {
            @Override
            public void onSuccess(BaseResBean body) {
                addressView.hideProgress();
                addressView.modifyAddressSuccess(body.getMessage());
            }

            @Override
            public void onFailure(int status, String msg) {
                addressView.hideProgress();
                if (status == 200){
                    addressView.modifyAddressSuccess(msg);
                }else {
                    addressView.modifyAddressFailed(msg);
                }

            }
        });
    }

    /**
     * 判断在该商铺下，选择的地址可不可配送
     * */
    public void judgeDelivery(final int addressId, String shopId, final String fullAddress, final String userInfo){
        addressView.showProgress();
        GXDrugModule.getInstance().judgeDelivery(addressId, shopId, new BaseHttpObserver<BaseResBean<AddressBackPriceBean.ResultBean>>() {
            @Override
            public void onSuccess(BaseResBean<AddressBackPriceBean.ResultBean> addressBackPriceBean) {
                addressView.hideProgress();
                mAddressBackPriceBean = addressBackPriceBean.getResult();
                if (mAddressBackPriceBean != null){
                    addressView.addressBackPriceSuccess(mAddressBackPriceBean,addressId,fullAddress,userInfo);
                }

            }

            @Override
            public void onFailure(int i, String s) {
                addressView.hideProgress();
                addressView.addressBackPriceFailed(s);
            }
        });
    }

    /**
     * 修改默认地址
     * */
    public void modifyDefaultAddress(final int addressId){
        GXDrugModule.getInstance().modifyDefaultAddress(addressId, new BaseHttpObserver<BaseResBean>() {
            @Override
            public void onSuccess(BaseResBean body) {
                addressView.modifyDefaultAddressSuccess(body.getMessage());
            }

            @Override
            public void onFailure(int status, String msg) {
                if (status == 200){
                    addressView.modifyDefaultAddressSuccess(msg);
                }else {
                    addressView.modifyDefaultAddressFailed(msg);
                }
            }
        });

    }

    /**
     * 校验手机
     */
    public boolean isMobile(final String str) {
        Pattern p = null;
        Matcher m = null;
        boolean b = false;
        /** 验证手机号 */
        p = Pattern.compile("^((13[0-9])|(15[0-9])|(16[0-9])|(17[0-8])|(18[0-9])|(19[0-9])|(14[0-9]))\\d{8}$");
        m = p.matcher(str);
        b = m.matches();
        return b;
    }

}
